﻿
namespace Numbrella.WebClient
{
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.Windows;
    using System.Windows.Controls;
    using Numbrella.Tracker.WebService;

    public partial class TestPage : UserControl
    {
        public TestPage()
        {
            InitializeComponent();

            UserToken userToken = App.Current.UserToken;

            if (userToken != null)
            {                
                userBlock.Text = String.Format("You are {0}.\nYour tokenID='{1}'.", userToken.Username, userToken.TokenID);
            }
        }

        private void OnTestButton_Click(object sender, RoutedEventArgs e)
        {
            IProblemsService problemsService 
                = new ChannelFactory<IProblemsService>("BasicHttpBinding_IProblemsService").CreateChannel();            
            
            AsyncCallback asyncCallBack = 
                delegate(IAsyncResult result)
                {
                    IList<Problem> problems = ((IProblemsService)result.AsyncState).EndGetProblems(result);

                    this.Dispatcher.BeginInvoke(
                        delegate
                            {
                                problemsList.ItemsSource = problems;
                            });                    
                };
            
            problemsService.BeginGetProblems(asyncCallBack, problemsService);
        }

        private void OnLoginLink_Click(object sender, RoutedEventArgs e)
        {
            Main main = this.Parent as Main;
            main.Navigate(new LoginPage());
        }
    }
}